package com.telenav.snowsweeper.renderer;

import com.telenav.snowsweeper.data.Point;

import android.app.Activity;
import android.graphics.Rect;
import android.os.SystemClock;
import android.view.Display;

public class Util {
	public static int screenWidth;
	public static int screenHeight;
	public static int canvasWidth;
	public static int canvasHeight;
	
	public static final int SQUARE_POWER = 6;
	public static final int ROW = 6;
	public static final int COLUMN = 8;
	public static final int SQUARE_NORMALIZED_SIZE = 1 << SQUARE_POWER;
	public static final int SQUARE_HALF_NORMALIZED_SIZE = SQUARE_NORMALIZED_SIZE >> 1;
	public static final int EDGE_MAP_MIN_X = 0;
	public static final int EDGE_MAP_MAX_X = SQUARE_NORMALIZED_SIZE * COLUMN;
	public static final int EDGE_MAP_MIN_Y = 0;
	public static final int EDGE_MAP_MAX_Y = SQUARE_NORMALIZED_SIZE * ROW;
	public static final int EDGE_MIN_SCALE = 128;
	public static final int EDGE_MAX_SCALE = 256; 
	
	public static final int GAME_MAX_LEVEL = 5;
	
	public static void init(Activity context) {
		android.graphics.Point p = new android.graphics.Point();
		Display play = context.getWindowManager().getDefaultDisplay();
		play.getSize(p);
		screenWidth = p.x;
		screenHeight = p.y;
	}
	
	public static int x2TileColumn(int x) {
		int index = x >> SQUARE_POWER;
		if (index < 0)
			index = 0;
		else if (index >= COLUMN)
			index = COLUMN - 1;
		return index;
	}
	
	public static int y2TileRow(int y) {
		int index = y >> SQUARE_POWER;
		if (index < 0)
			index = 0;
		else if (index >= ROW)
			index = ROW - 1;
		return index;
	}
	
	public static int x2PointColumn(int x) {
		return x >> SQUARE_POWER; 
	}
	
	public static int y2PointRow(int y) {
		return y >> SQUARE_POWER;
	}
	
	public static void mapPoint2ScreenPoint(Point mapPoint, Rect screenRect, int scaleX, int scaleY, long timeStamp) {
		int x = mapDist2Screen(mapPoint.getX() - screenRect.left, scaleX);
		int y = mapDist2Screen(mapPoint.getY() - screenRect.top, scaleY);
		mapPoint.setScreenXY(x, y, timeStamp);
	}
	
	public static float getPanSensitive() {
		return 1.5f;
	}
	
	public static float getZoomSensitive() {
		return 5.0f;
	}
	
	public static int mapDist2Screen(int dist, int scale) {
		return dist * scale + Util.SQUARE_HALF_NORMALIZED_SIZE >> Util.SQUARE_POWER;
	}
	
	public static int screenDist2Map(int dist, int scale) {
		return ((dist << Util.SQUARE_POWER) + (scale >> 1)) / scale;
	}
	
	public static boolean isPointsClosed(Point point1, Point point2) {
		int row1 = point1.getRow();
		int col1 = point1.getColumn();
		int row2 = point2.getRow();
		int col2 = point2.getColumn();
		return Math.abs(col1 - col2) == 1 && row1 == row2 || Math.abs(row1 - row2) == 1 && col1 == col2;
	}
	
	public static long beginTime;
	public static void resetTime() {
		beginTime = SystemClock.uptimeMillis();
	}

	public static void updateTime() {
		
	}
	
	public static int getValue(int value, int max, int min) {
		if (value > max)
			value = max;
		else if (value < min)
			value = min;
		return value;
	}
}
